package com.qiqu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiqu.Vo.ArticleVo;
import com.qiqu.dao.mapper.ArticleMapperCustom;
import com.qiqu.entity.Article;
import com.qiqu.dao.ArticleDao;
import com.qiqu.entity.Comments;
import com.qiqu.service.ArticleService;
import com.qiqu.utils.PagedResult;
import org.n3r.idworker.Sid;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Article)表服务实现类
 *
 * @author makejava
 * @since 2021-04-12 20:47:34
 */
@Service("articleService")
public class ArticleServiceImpl implements ArticleService {
    @Resource
    private ArticleDao articleDao;
    @Resource
    private Sid sid;
    @Resource
    private ArticleMapperCustom articleMapperCustom;


    @Override
    public Article queryById(String id) {
        Article article=new Article();
        article.setId(id);
        return articleDao.selectOne(article);
    }

    @Override
    public Article insert(Article article) {
        article.setId(sid.nextShort());
        articleDao.insert(article);
        return article;
    }

    @Override
    public Article update(Article article) {
        return null;
    }

    @Override
    public boolean deleteById(String id) {
        Example c = new Example(Article.class);
        Example.Criteria criteria = c.createCriteria();
        criteria.andEqualTo("id",id);
        return articleDao.deleteByExample(c)>0;
    }

    @Override
    public PagedResult getPersonArticle(String userId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ArticleVo> articleVos= articleMapperCustom.queryArticleByUserId(userId);
        PageInfo<ArticleVo> pageList=new PageInfo<>(articleVos);
        PagedResult pagedResult =new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(articleVos);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Override
    public PagedResult getArticle(Integer page, Integer pageSize, String keyword) {
        PageHelper.startPage(page,pageSize);
        List<ArticleVo> articleVos = new ArrayList<>();
        if(keyword==null || keyword.equals("")){
            articleVos= articleMapperCustom.queryArticle();
        }else {
            articleVos = articleMapperCustom.queryArticleByKeyword(keyword);
        }
        PageInfo<ArticleVo> pageList=new PageInfo<>(articleVos);
        PagedResult pagedResult =new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(articleVos);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Override
    public PagedResult getArticleNear(Integer page, Integer pageSize, String keyword, String place) {
        PageHelper.startPage(page,pageSize);
        List<ArticleVo> articleVos = new ArrayList<>();
        if(keyword==null || keyword.equals("")){
            articleVos= articleMapperCustom.queryArticleByNear(place);
        }else {
            articleVos = articleMapperCustom.queryArticleByNearKeyword(keyword,place);
        }
        PageInfo<ArticleVo> pageList=new PageInfo<>(articleVos);
        PagedResult pagedResult =new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(articleVos);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Override
    public PagedResult getArticleAll(Integer page, Integer pageSize, String keyword, String place, String tag, String userId) {
        PageHelper.startPage(page,pageSize);
        List<ArticleVo> articleVos = articleMapperCustom.queryArticle1(place,keyword,userId,tag);
        PageInfo<ArticleVo> pageList=new PageInfo<>(articleVos);
        PagedResult pagedResult =new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(articleVos);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Override
    public PagedResult userLikeUPArticle(String userId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ArticleVo> articleVos = articleMapperCustom.queryUserGZ(userId);
        PageInfo<ArticleVo> pageList=new PageInfo<>(articleVos);
        PagedResult pagedResult =new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(articleVos);
        pagedResult.setRecords(pageList.getTotal());

        return pagedResult;
    }

    @Override
    public PagedResult userLikeArticle(String userId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ArticleVo> articleVos = articleMapperCustom.queryUserLikeArticle(userId);
        PageInfo<ArticleVo> pageList=new PageInfo<>(articleVos);
        PagedResult pagedResult =new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(articleVos);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Override
    public PagedResult userCollection(String userId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ArticleVo> articleVos = articleMapperCustom.queryUserCollection(userId);
        PageInfo<ArticleVo> pageList=new PageInfo<>(articleVos);
        PagedResult pagedResult =new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(articleVos);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }

    @Override
    public PagedResult getArticleLikeCounts(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ArticleVo> articleVos = articleMapperCustom.queryArticleLikeCounts();
        PageInfo<ArticleVo> pageList=new PageInfo<>(articleVos);
        PagedResult pagedResult =new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(articleVos);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }


    public static void main(String[] args){
        System.out.println("hello");
    }

}